﻿using System.Collections.Generic;

namespace Vcm
{
    public static class DictionaryExtensions
    {
        /// <summary>
        /// Gets the value matching the key in the dictionary, or the default value
        /// if the key doesn't exist.
        /// </summary>
        /// <typeparam name="TK">The key type.</typeparam>
        /// <typeparam name="TV">The value type.</typeparam>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="key">The key searched in the dictionary.</param>
        /// <param name="defaultValue">The default value to return if the key doesn't exist in the dictionary.</param>
        /// <returns>The value indexed by the key, or the default value if the key doesn't exist in the dictionary.</returns>
        public static TV ValueOrDefault<TK, TV>(this IDictionary<TK, TV> dictionary, TK key, TV defaultValue = default(TV))
        {
            TV result;
            return dictionary.TryGetValue(key, out result)
                ? result
                : defaultValue;
        }
    }
}
